﻿@model DeliveryPlus.Web.DAL.Entities.Order

@{
    ViewBag.Title = "Оформление заказа";
    Layout = "../Shared/Layout.cshtml";
}

<div id="page-checkout" class="page">
    <div class="title">
        <img src="~/Content/images/order/title.png" />    
    </div>
    <table id="checkout-table">
        <thead>
            <tr>
                <th>
                    Блюдо
                </th>
                <th>Количество</th>
                <th>Сумма</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var orderItem in Model.OrderItems)
            {
                <tr data-id="@orderItem.Id" data-price="@orderItem.Price">
                    <td class="name">
                        <div class="dish-photo">
                            @if (!String.IsNullOrEmpty(orderItem.Dishe.Photo))
                            {
                                <img src="/Files/Dish/@orderItem.Dishe.Photo" alt="@orderItem.Dishe.Title, @orderItem.Dishe.Cafe.Title, Хабаровск"/>
                            }
                        </div>
                        <div class="dish-title">@orderItem.Dishe.Title</div>
                    </td>
                    <td>
                        <input class="quantity-selector" type="text" value="@orderItem.Quantity"/>
                    </td>
                    <td class="price-holder-cell">
                        <span class="price-holder">
                            @String.Format("{0:c}",orderItem.Quantity*orderItem.Price)    
                        </span>
                        <span class="delete" title="Удалить позицию"></span>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <div id="total">
        <span id="total-label">Итого без учета доставки:</span>
        <span id="total-wrapper">
            @String.Format("{0:c}",@Model.OrderItems.Sum(oi => oi.Quantity*oi.Price))
        </span>
    </div>
    <img src="~/Content/images/cafe/hr.png" />
    <div id="order-form-wrapper">
        <div id="order-form-bg"></div>
        <div id="order-form-title">
            Пожалуйста, заполните данные:
        </div>
        <form action="/do-checkout" id="checkout-form" method="POST">
            <div class="field">
                <input type="text" name="name" class="name f" placeholder="Ваше имя*"/>
            </div>
            <div class="field">
                <input type="text" name="phone" class="phone f" placeholder="Контактный телефон*"/>
            </div>
            <div class="field">
                <input type="text" name="email" class="email f" placeholder="E-mail*"/>
            </div>
            <div class="field">
                <input type="text" name="address" class="address f" placeholder="Улица дом, квартира, этаж*"/>
            </div>
            <div class="field">
                <textarea name="comments" class="comments f" placeholder="Комментарии к заказу"></textarea>
            </div>
            <div class="submit">
                <input type="submit" value=" "/>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        function recalc() {
            var total = 0.0;
            $("#checkout-table tbody tr").each(function(index, item) {
                var id = parseInt($(item).data("id"));
                var price = parseFloat($(item).data("price").replace(",", "."));
                var quantity = parseInt($(item).find(".quantity-selector").val());
                if (isNaN(quantity) || quantity <= 0) {
                    quantity = 1;
                    $(item).find(".quantity-selector").val(1);
                }
                var t = price * quantity;
                total += t;
                var s = accounting.formatMoney(t, "руб.", 2, " ", ",", "%v %s");
                $(item).find(".price-holder").text(s);
            });
            $("#total-wrapper").text(accounting.formatMoney(total, "р.", 2, " ", ",", "%v %s"));
        }

        recalc();

        $(".quantity-selector").bind("change", function () {
            var item = $(this).parent().parent();
            var id = parseInt($(item).data("id"));
            var quantity = parseInt($(item).find(".quantity-selector").val());
            if (isNaN(quantity) || quantity <= 0) {
                quantity = 1;
                $(item).find(".quantity-selector").val(1);
            }
            $.post("/change-quantity", { id: id, quantity: quantity }, function(data) {
                if (data.success == true) {
                    recalc();
                } else {
                    alert(data.msg);
                }
            },"json");
        });

        $(".delete").click(function() {
            var item = $(this).parent().parent();
            var id = parseInt($(item).data("id"));
            $.post("/delete-dish", { id: id }, function(data) {
                if (data.success == true) {
                    $(item).remove();
                    recalc();
                } else {
                    alert(data.msg);
                }
            }, "json");
        });

        $("#checkout-form").submit(function() {
            var fields = $("#checkout-form .f");
            for (var i = 0; i < fields.length; i++) {
                var field = fields[i];
                var val = $(field).val();
                if (val == null || val == "" || val.trim() == "") {
                    alert("Пожалуйста, заполните все поля формы заказа");
                    return false;
                }
            }
            return true;
        });
    });
</script>